import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';

// 自动收集各个业务模块下export.ts中的页面路由配置
const files: any = import.meta.glob('../view-modules/**/router.ts', { eager: true });
// 引入登录注册的路由配置
import { loginRoutes } from '@/login-and-register/router';

const layoutRoute: RouteRecordRaw = {
	path: '/',
	name: 'layout',
	component: () => import('@/global-layout-ui/Layout.vue'),
	children: [],
	redirect: ''
};

Object.keys(files).forEach((fileKey) => {
	const routes = files[fileKey]?.default;
	if (routes && JSON.stringify(routes) !== '{}') {
		Array.isArray(routes) ? layoutRoute.children.push(...routes) : layoutRoute.children.push(routes);
	}
});

const Router = createRouter({
	history: createWebHistory(),
	routes: [
		...loginRoutes,
		layoutRoute,
		{
			name: 'monitoring-center-situation',
			path: '/monitoring-center/situation',
			component: () => import('../view-modules/monitoring-center/pages/situation/situation.vue'),
			meta: { noPermission: true }
		},
		{ path: '/403', name: '403', component: (): any => import('@/error-views/403.vue'), meta: { noPermission: true } },

		{ path: '/404', name: '404', component: (): any => import('@/error-views/404.vue'), meta: { noPermission: true } },

		{ path: '/:pathMatch(.*)', redirect: '/404' }
	]
});

export default Router;
